package com.shuzixiangcun.controller.admin;

import com.github.pagehelper.PageInfo;
import com.shuzixiangcun.entity.Orders;
import com.shuzixiangcun.entity.Products;


import com.shuzixiangcun.entity.Users;
import com.shuzixiangcun.service.admin.ProductsServiceImpl;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Products)表控制层
 *
 * @author makejava
 * @since 2024-06-28 11:00:55
 */
@RestController
@RequestMapping("admin/products")
public class ProductsController {
    /**
     * 服务对象
     */
    @Resource
    private ProductsServiceImpl productsService;



    @GetMapping("query")
    public ResponseEntity<List<Products>> getProducts() {
        return ResponseEntity.ok(productsService.queryList());
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("queryById")
    public ResponseEntity<Products> queryById(Integer id) {
        return ResponseEntity.ok(this.productsService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param products 实体
     * @return 新增结果
     */
    @PostMapping("add")
    public ResponseEntity add(Products products) {
        return ResponseEntity.ok(this.productsService.insert(products));
    }

    /**
     * 编辑数据
     *
     * @param products 实体
     * @return 编辑结果
     */


    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */

    @GetMapping("/List")
    public ResponseEntity<PageInfo<Products>> showList(@RequestParam Integer pageNo, @RequestParam Integer pageSize, @RequestParam(required = false) String search) {
        return ResponseEntity.ok(this.productsService.getPageInfo(pageNo,pageSize,search));
    }
    @PostMapping("update")
    public ResponseEntity update(Products products){

        String state1=products.getState().equals("1")?"0":"1";

        products.setState(state1);
        if (this.productsService.update(products)>0){
            return ResponseEntity.status(200).header("Custom-Header", "foo")
                    .body("修改成功");
        }
        return ResponseEntity.status(500).header("Custom-Header", "foo")
                .body("修改失败");
    }
    @DeleteMapping("delete")
    public ResponseEntity deleteById(Integer id) {
        return ResponseEntity.ok(this.productsService.deleteById(id));
    }

    @PostMapping("update3")
    public ResponseEntity update3(Products products){
        return ResponseEntity.ok(this.productsService.update3(products));
    }

}

